package com.feng.cloud.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.cloud.common.constants.SystemConstant;
import com.feng.cloud.common.constants.request.RequestParamsConstant;
import com.feng.cloud.common.request.QueryRequest;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName SortUtils
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/23 14:06
 * @Version 1.0
 **/
@UtilityClass
public class SortUtils {

    /**
     * 处理时间区间查询
     * @param request QueryRequest
     * @param fn SFunction
     * @param queryWrapper LambdaQueryWrapper
     * @param <T> 实体类型
     */
    public  <T> void handleTimeFromTo(QueryRequest request, SFunction<T, ?> fn, LambdaQueryWrapper<T> queryWrapper) {
        String timeFrom = ClassCastUtils.toString(request.get(RequestParamsConstant.CREATE_TIME_FROM));
        String timeTo= ClassCastUtils.toString(request.get(RequestParamsConstant.CREATE_TIME_TO));
        if (StringUtils.isNotBlank(timeFrom) && StringUtils.isNotBlank(timeTo)) {
            queryWrapper.between(fn, timeFrom, timeTo);
        }
    }

    /**
     * 处理排序（单表分页情况下）,默认开启开启驼峰转下划线 for mybatis-plus
     * @param request QueryRequest
     * @param page page
     * @param fn 默认排序的字段
     * @param defaultOrder 默认排序规则
     * @param <T> T
     */
    public <T> void handlePageAndSort(QueryRequest request, Page<?> page, SFunction<T, ?> fn, String defaultOrder) {
        handlePageAndSort(request,page,fn,defaultOrder,true);
    }
    /**
     * 处理排序（多表分页情况下） for mybatis-plus
     * @param request QueryRequest
     * @param page page
     * @param fn 默认排序的字段
     * @param defaultOrder 默认排序规则
     * @param camelToUnderscore 是否驼峰转下划线
     * @param <T> T
     */
    public <T> void handlePageAndSort(QueryRequest request, Page<?> page, SFunction<T, ?> fn, String defaultOrder, boolean camelToUnderscore) {
        page.setCurrent(request.getPageNum());
        page.setSize(request.getPageSize());
        String field = request.getField();
        String order = request.getOrder();
        if (StringUtils.isBlank(field)) {
            // 使用默认排序字段
            field = LambdaUtils.getProperty(fn);
        }
        // 驼峰转下划线
        if (camelToUnderscore) {
            field = WebUtils.camelToUnderscore(field);
        }
        if (StringUtils.isBlank(order)) {
            // 使用默认排序规则
            order = defaultOrder;
        }
        if (StringUtils.equals(SystemConstant.ORDER_ASC, order)) {
            page.addOrder(OrderItem.asc(field));
        } else {
            page.addOrder(OrderItem.desc(field));
        }
    }

    /**
     * 处理排序（分页情况下） for mybatis-plus
     *
     * @param request           QueryRequest
     * @param page              Page
     * @param defaultSort       默认排序的字段
     * @param defaultOrder      默认排序规则
     * @param camelToUnderscore 是否开启驼峰转下划线
     */
    public void handlePageSort(QueryRequest request, Page<?> page, String defaultSort, String defaultOrder, boolean camelToUnderscore) {
        page.setCurrent(request.getPageNum());
        page.setSize(request.getPageSize());
        String sortField = request.getField();
        if (camelToUnderscore) {
            sortField = WebUtils.camelToUnderscore(sortField);
            defaultSort = WebUtils.camelToUnderscore(defaultSort);
        }
        if (StringUtils.isNotBlank(sortField)
                && StringUtils.isNotBlank(request.getOrder())
                && !StringUtils.equalsIgnoreCase(request.getField(), "null")
                && !StringUtils.equalsIgnoreCase(request.getOrder(), "null")) {
            if (StringUtils.equals(request.getOrder(), SystemConstant.ORDER_DESC))
                page.addOrder(OrderItem.desc(sortField));
            else
                page.addOrder(OrderItem.asc(sortField));
        } else {
            if (StringUtils.isNotBlank(defaultSort)) {
                if (StringUtils.equals(defaultOrder, SystemConstant.ORDER_DESC))
                    page.addOrder(OrderItem.desc(defaultSort));
                else
                    page.addOrder(OrderItem.asc(defaultSort));
            }
        }
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request QueryRequest
     * @param page    Page
     */
    public void handlePageSort(QueryRequest request, Page<?> page) {
        handlePageSort(request, page, null, null, false);
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request           QueryRequest
     * @param page              Page
     * @param camelToUnderscore 是否开启驼峰转下划线
     */
    public void handlePageSort(QueryRequest request, Page<?> page, boolean camelToUnderscore) {
        handlePageSort(request, page, null, null, camelToUnderscore);
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request           QueryRequest
     * @param wrapper           wrapper
     * @param defaultSort       默认排序的字段
     * @param defaultOrder      默认排序规则
     * @param camelToUnderscore 是否开启驼峰转下划线
     */
    public void handleWrapperSort(QueryRequest request, QueryWrapper<?> wrapper, String defaultSort, String defaultOrder, boolean camelToUnderscore) {
        String sortField = request.getField();
        if (camelToUnderscore) {
            sortField = WebUtils.camelToUnderscore(sortField);
            defaultSort = WebUtils.camelToUnderscore(defaultSort);
        }
        if (StringUtils.isNotBlank(request.getField())
                && StringUtils.isNotBlank(request.getOrder())
                && !StringUtils.equalsIgnoreCase(request.getField(), "null")
                && !StringUtils.equalsIgnoreCase(request.getOrder(), "null")) {
            if (StringUtils.equals(request.getOrder(), SystemConstant.ORDER_DESC))
                wrapper.orderByDesc(sortField);
            else
                wrapper.orderByAsc(sortField);
        } else {
            if (StringUtils.isNotBlank(defaultSort)) {
                if (StringUtils.equals(defaultOrder, SystemConstant.ORDER_DESC))
                    wrapper.orderByDesc(defaultSort);
                else
                    wrapper.orderByAsc(defaultSort);
            }
        }
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request QueryRequest
     * @param wrapper wrapper
     */
    public void handleWrapperSort(QueryRequest request, QueryWrapper<?> wrapper) {
        handleWrapperSort(request, wrapper, null, null, false);
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request           QueryRequest
     * @param wrapper           wrapper
     * @param camelToUnderscore 是否开启驼峰转下划线
     */
    public void handleWrapperSort(QueryRequest request, QueryWrapper<?> wrapper, boolean camelToUnderscore) {
        handleWrapperSort(request, wrapper, null, null, camelToUnderscore);
    }
}
